package com.xique.door.project.miniprogram.controller;

import com.xique.common.core.enums.StatusCode;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shd
 * @create 2022/4/22 11:42
 */
@RestController
@RequestMapping("/common")
public class MiniCommonController extends BaseController {

    @Autowired
    private AliossUtil aliossUtil;

    @ResponseBody
    @PostMapping("/uploadImage")
    @Log(title = "oss导入", businessType = BusinessType.IMPORT)
    @ApiOperation("oss文件导入")
    public AjaxResult reduceSizeAndUploadImage(@RequestParam("file") MultipartFile file) {
        if (file != null) {
            Map<String,Object> resultMap = aliossUtil.reduceImageSizeAndUpload(file,100);
            if (resultMap.get("code").equals(StatusCode.ERROR.getCode())) {
                return AjaxResult.error("error","文件上传失败！！！");
            }
            AjaxResult result = AjaxResult.success("success", "文件上传成功");
            Map<String, Object> map = new HashMap<>(2);
            map.put("url", resultMap.get("url"));
            map.put("objectName", resultMap.get("objectName"));
            result.put(AjaxResult.DATA_TAG, map);

            return result;
        } else {
            return AjaxResult.error("error","文件上传失败！！！");
        }
    }
}
